package com.itheima.test02;

import java.util.concurrent.CountDownLatch;

/*
题目二:
	请使用CountDownLatch编写一个程序，实现以下效果：
	线程A打印：”开始计算”
	线程B：计算1--100所有数的累加和，并打印结果。
	线程A打印：”计算完毕”
 */
public class Test {
    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() + "开始计算");
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "计算完毕");
            }
        },"线程A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int sum = 0;
                for (int i = 1; i < 100+1; i++) {
                    sum += i;
                }
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + "计算1-100累加和：" + sum);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"线程B").start();
    }
}
